<import file="block/common.html" name="url"/>
<div class="layui-fluid">
	<div class="layui-row layui-col-space15">
		<div class="layui-col-md12">
			<script type="text/html" id="toolbarAE">
            <div class="layui-btn-container">
			<?php echo sysrolTF("admingroup_add")?' <button class="layui-btn layui-btn-sm data-add-btn">'.lang('add').' </button>':''; ?> 
            </div>
        </script>

			<table class="layui-hide" id="currentTableId" lay-data="{id: 'idReload'}" lay-filter="currentTableFilter">
			</table>

			<script type="text/html" id="switchTpl">
		  <input type="checkbox" name="status" id="{{d.id}}" value="{{d.status}}" lay-skin="switch" lay-text="启|禁用" lay-filter="switchStatus" {{ d.status == 1 ? 'checked' : '' }}>
		</script>

			<script type="text/html" id="currentTableBar">
		<?php echo sysrolTF("admingroup_edit")?' <a class="layui-btn layui-btn-xs data-count-edit" lay-event="edit">'.lang('edit').'</a>':''; ?> 
		<?php echo sysrolTF("admingroup_del")?'<a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">'.lang('del').'</a>':''; ?> 
        </script>
		</div>
	</div>

</div>

<script>
	layui.use('table', function () {
		var table = layui.table,
			$ = layui.jquery,
			form = layui.form;
		table.render({
			elem: '#currentTableId',
			//url: '/res/admin/api/table.json',
			url: '<{caUrl("adminuser","index")}>&list_json',
			toolbar: '#toolbarAE',
			defaultToolbar: ['filter', 'exports', 'print', {
				title: '提示:管理员帐号请不要随意改动！',
				layEvent: 'LAYTABLE_TIPS',
				icon: 'layui-icon-tips'
			}],
			page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
				layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']//自定义分页布局
				//,curr: 5 //设定初始在第 5 页
				, groups: 1 //只显示 1 个连续页码
				, first: false //不显示首页
				, last: false //不显示尾页
			},
			cols: [[
				//{type: "checkbox", width: 50, fixed: "left"},
				{ field: 'id', width: 80, title: 'ID', sort: true },
				{ field: 'username', title: '登录名' },
				{ field: 'groupname', title: '角色' },
				{ field: 'nickname', width: 320, title: '真实姓名' },
				{ field: 'loginip', width: 320, title: '最后登录IP' },
				{ field: 'logintime', width: 200, title: '最后登录', sort: true },
				{ field: 'status', width: 200, title: '状态', sort: true, templet: '#switchTpl', unresize: true },
				{ title: '操作', minWidth: 50, templet: '#currentTableBar', fixed: "right", align: "center" }
			]],
			limits: [10, 15, 20, 25, 50, 100],
			limit: 10,
			page: true
		});


		// 监听添加操作
		$(".data-add-btn").on("click", function () {
			loadMain('<{caUrl("adminuser","add")}>&view&menuname=添加管理员');
			return false;
		});
		// 监听删除操作
		$(".data-delete-btn").on("click", function () {
			var checkStatus = table.checkStatus('currentTableId')
				, data = checkStatus.data;
			layer.alert(JSON.stringify(data));
		});

		//监听表格复选框选择
		table.on('checkbox(currentTableFilter)', function (obj) {
			console.log(obj)
		});

		//监听状态操作
		form.on('switch(switchStatus)', function (obj) {
			var data = obj.data;
			console.log(this.id);
			var checked = 0;
			if (obj.elem.checked) {
				checked = 1;
			}
			$.post('<{caUrl("adminuser","switchStatus")}>', { id: this.id, status: checked }, function (res) {
				if (res.status == 1) {
					layer.tips(res.info + ' 状态为：' + checked, obj.othis);
				} else {
					layer.msg(res.info, { icon: 2, anim: 6, time: 5000 });
				}
			});
		});
		table.on('tool(currentTableFilter)', function (obj) {
			var data = obj.data;
			if (obj.event === 'edit') {
				loadMain('<{caUrl("adminuser","edit")}>&view&menuname=编辑管理员&id=' + data.id);
				return false;
			} else if (obj.event === 'delete') {
				layer.confirm('真的删除么', function (index) {
					$.post('<{caUrl("adminuser","del")}>', data, function (res) {
						if (res.status == 1) {
							layer.msg(res.info, { icon: 1, time: 1000 }, function () {
								obj.del();
								layer.close(index);
							});
						} else {
							layer.msg(res.info, { icon: 2, anim: 6, time: 5000 });
						}
					});
				});
			}
		});
	});
</script>